<% html_title("DMSF") %>

<div class="contextual">
	<% if User.current.allowed_to?(:folder_manipulation, @project) %>
		<% unless @folder.nil? %>
			<%= link_to("Details",
		          {:controller => "dmsf_detail", :action => "folder_detail", :id => @project, :folder_id => @folder}) %> |
		<% end %>
		<% form_for(DmsfFolder.new, :url => {:controller => "dmsf_detail", :action => "create_folder", :id => @project, :folder_id => @folder}, :html => {:method=>:post}) do |f| %>
			Title: <%= f.text_field(:name) %><%= f.submit("Create folder") %>
		<% end %>
	<% end %>
</div>

<% path = @folder.nil? ? [] : @folder.dmsf_path %>
<h2>
	<%= render(:partial => 'path', :locals => {:path => path}) %>
</h2>

<div class="wiki">
<%= textilizable(@folder.description) unless @folder.nil? %>
</div>

<%
form_tag({:action => "entries_operation", :id => @project, :folder_id => @folder}, :method => :post, 
	:class => "dmfs_entries", :id => "Entries") do
%>
	<%= hidden_field_tag("action") %> 
	<table class="list entries" id="browser">
		<thead>
			<tr id="root">
				<th style="width: 17px; padding: 2px; text-align: left;">
					<input id="check_all_entries" title="Check/Uncheck all for zip download or email" type="checkbox" />
				</th>
				<%= sort_header_tag("title", :caption => "Title") %>
				<%= sort_header_tag("size", :caption => "Size") %>
				<%= sort_header_tag("modified", :caption => "Modified") %>
				<%= sort_header_tag("version", :caption => "Ver.") %>
				<%= sort_header_tag("author", :caption => "Author") %>
				<th></th>
			</tr>
		</thead>
	<tbody>
	<% @subfolders.each do |subfolder| %>
		<tr class="dir">
			<td class="size"><%= check_box_tag("subfolders[]", subfolder.id, false, :title => "Check for multi download or email") %></td>
			<td class="filename">
				<%= link_to(h(subfolder.name),
		          {:action => "index", :folder_id => subfolder},
		          :class => "icon icon-folder") %>
			</td>
			<td class="size">-</td>
			<td class="modified">-</td>
			<td class="version">-</td>
			<td class="author">-</td>
			<td class="actions">
				<div class="right_icon_box">
					<% if User.current.allowed_to?(:folder_manipulation, @project) %>
						<%= link_to(image_tag("delete.png", :plugin => "redmine_dmsf"), 
							{:controller => "dmsf_detail", :action => "delete_folder", :id => @project, 
							:folder_id => @folder, :delete_folder_id => subfolder}, :class => "delete-link",
							:title => "Delete") %>
					<% end %>
					<% if User.current.allowed_to?(:file_approval, @project) %>
						<% if subfolder.notification %>
							<%= link_to(image_tag("notify.png", :plugin => "redmine_dmsf"),
				        		{:controller => "dmsf_state", :action => "folder_notify_deactivate", :id => @project,
								:folder_id => subfolder}, :title => "Notifications active: Deactivate") %>
						<% else %>
							<%= link_to(image_tag("notifynot.png", :plugin => "redmine_dmsf"),
				        		{:controller => "dmsf_state", :action => "folder_notify_activate", :id => @project, 
								:folder_id => subfolder}, :title => "Notifications not active: Activate") %>
						<% end %>
					<% end %>
				</div><br class="clear" />
			</td>
		</tr>
	<% end %>
	<% @files.each do |file| %>
		<tr class="file">
			<td class="size"><%= check_box_tag("files[]", file.id, false, :title => "Check for zip download or email") %></td>
			<td class="filename">
				<%= link_to(h(file.last_revision.display_title),
		        	{:action => "download_file", :id => @project, :file_id => file},
		        	:class => "icon icon-file #{Redmine::MimeType.css_class_of(file.name)}",
					:title => "#{h(file.last_revision.title)} version #{file.last_revision.version} download") %>
				<div class="filename">
					(<%= h(file.display_name) %>)</div><br class="clear" />
			</td>
			<td class="size"><%= number_to_human_size(file.last_revision.size) unless file.last_revision.nil?  %></td>
			<td class="modified">
				<%= file.last_revision.updated_at.strftime("%Y-%m-%d %H:%M") unless file.last_revision.nil? %>
				<% if file.locked_for_user? %>
					<%= link_to(image_tag("locked.png", :plugin => "redmine_dmsf"), 
						{:controller => "users", :action => "show", :id => file.locks[0].user },
						:title => "Locked by " + file.locks[0].user.to_s) %>
        		<% else 
					if file.locked? %>
					<%= image_tag("lockedbycurrent.png", :title => "Locked by you", 
						:plugin => "redmine_dmsf") %>
				<% 	end %>
				<% end %>
			</td>
			<td class="version">
				<%= file.last_revision.version unless file.last_revision.nil? %>
				<% case file.last_revision.workflow
        				when 1 then %><%= image_tag("waitingforapproval.png", :title => "Waiting for Approval",
											:plugin => "redmine_dmsf") %>
        		<% 		when 2 then %><%= image_tag("approved.png", :title => "Approved",
											:plugin => "redmine_dmsf") %>
				<% end %>
			</td>
			<td class="author"><%= h(file.last_revision.user) unless file.last_revision.nil? %></td>
			<td class="actions">
				<div class="right_icon_box">
					<%= link_to(image_tag("filedetails.png", :plugin => "redmine_dmsf"), 
						{:controller => "dmsf_detail", :action => "file_detail", :id => @project, :file_id => file },
						:title => "#{h(file.last_revision.title)} details") %>
					&nbsp;	
					<% unless file.locked_for_user? && !User.current.allowed_to?(:force_file_unlock, @project)%>
						<% 	if file.locked? %>
							<%= link_to(image_tag("unlock.png", :plugin => "redmine_dmsf"),
					        	{:controller => "dmsf_state", :action => "unlock_file", :id => @project, :file_id => file },
								:title => "Unlock to allow changes for other members") %>
						<% 	else %>
							<%= link_to(image_tag("lock.png", :plugin => "redmine_dmsf"),
					        	{:controller => "dmsf_state", :action => "lock_file", :id => @project, :file_id => file },
								:title => "Lock to prevent changes for other members") %>
						<% 	end %> 
						&nbsp;
					<% end %>
					<% if User.current.allowed_to?(:file_manipulation, @project) %>
						<%= link_to(image_tag("delete.png", :plugin => "redmine_dmsf"), 
							{:controller => "dmsf_detail", :action => "delete_file", :id => @project, 
							:file_id => file}, :class => "delete-link", :title => "Delete") %>
					<% end %>
					<% if User.current.allowed_to?(:file_approval, @project) %>
						<% if file.notification %>
							<%= link_to(image_tag("notify.png", :plugin => "redmine_dmsf"),
				        		{:controller => "dmsf_state", :action => "file_notify_deactivate", :id => @project, 
								:file_id => file}, :title => "Notifications active: Deactivate") %>
						<% else %>
							<%= link_to(image_tag("notifynot.png", :plugin => "redmine_dmsf"),
				        		{:controller => "dmsf_state", :action => "file_notify_activate", :id => @project, 
								:file_id => file}, :title => "Notifications not active: Activate") %>
						<% end %>
					<% end %>
				</div><br class="clear" />
			</td>
		</tr>
	<% end %>
	</tbody>
	</table>
	<div class="controls">
		<%= submit_tag("Download", :title => "Download checked in zip archive", :name => "download_entries") %>
		<!-- <button type="button" id="EntriesDeleteButton" title="Delete checked">Delete</button> -->
		<%= submit_tag("Email", :title => "Send checked by email", :name => "email_entries") %>
	</div>
	<br style="clear: both"/>
<% end %>

<div class="box">
<h3>File Upload</h3>
<% form_tag({:controller => "dmsf_detail", :action => "upload_files", :id => @project, :folder_id => @folder},
	:id => "uploadform", :method=>:post, :multipart => true) do %>
<div style="padding-bottom: 6px; line-height: 1.4em; font-size: 0.9em;"> 
<div class="upload_select">
	File size: <select id="uploader_select"><option value="1">&lt; 100 MB</option><option value="2">> 100 MB</option></select>
</div>
<span>There can be uploaded maximum of 20 files at once. To upload files greater than 2GB you must have 64b browser.</span>
<br style="clear: both" />
</div>
<div id="uploader">
<p>
<span id="uploaded_files_fields">
<%= file_field_tag("uploaded_files[1]", :size => 30, :id => nil) %>
</span>
<br />
<small><%= link_to(l(:label_add_another_file), "#", :onclick => "dmsfAddFileField(); return false;" ) %>
(<%= l(:label_max_size) %>: <%= number_to_human_size(Setting.attachment_max_size.to_i.kilobytes) %>)
</small>
</p>
<%= submit_tag("Upload") %>
</div>
<% end %>
</div> 

<%= render(:partial => 'user_pref') %>

<% content_for :header_tags do %>
	<%= stylesheet_link_tag "dmsf", :plugin => "redmine_dmsf" %>
	<%= stylesheet_link_tag "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/base/jquery-ui.css" %>
	<%= stylesheet_link_tag "plupload/jquery.ui.plupload.css", :plugin => "redmine_dmsf" %>
	<%= javascript_include_tag "https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" %>
	<%= javascript_include_tag "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js" %>
	<%= javascript_include_tag "plupload/gears_init.js", :plugin => "redmine_dmsf" %>
	<%= javascript_include_tag "plupload/plupload.full.min.js", :plugin => "redmine_dmsf" %>
	<%= javascript_include_tag "plupload/jquery.ui.plupload.js", :plugin => "redmine_dmsf" %>
	<script type="text/javascript">
		jQuery.noConflict();
		
		var originalUploaderContent;
		
		jQuery(document).ready(function() {
			jQuery("a.delete-link").click(function(event) {
				if(!window.confirm("<%= l(:question_do_you_really_want_to_delete_this_entry) %>")) event.preventDefault();
			})
			
			jQuery("#check_all_entries").click(function(event) {
				var $this = jQuery(this);
				if($this.attr("checked")) {
					jQuery("input[type=checkbox]", jQuery("#browser > tbody")).attr("checked", true);
				} else {
					jQuery("input[type=checkbox]", jQuery("#browser > tbody")).attr("checked", false);
				}
			})
			
			jQuery("#EntriesDeleteButton").click(function(event) {
				if(!window.confirm("Really?")) {
					event.preventDefault();
					return;
				}
				
				var operation = jQuery("input[name=EntriesOperation]");
				operation.val("delete");
				var entries = jQuery("#Entries");
				entries.submit();
			})
			
			var uploader = jQuery('#uploader');
			
			originalUploaderContent = uploader.html();
			
			jQuery('#uploader_select').change(function() {
				if(jQuery(this).val() == 2) {
					uploader.html(originalUploaderContent);
				} else {
					initPlUploader(uploader);
				}
			});
			
			initPlUploader(uploader);

		});
		
		function initPlUploader(uploader) {
			uploader.html("<div></div>");
			uploader = jQuery("div",uploader);
			uploader.plupload({
				runtimes : 'gears,html5,flash,html4',
				url : '<%= url_for({:controller => "dmsf_detail", :action => "upload_file", :id => @project, :folder_id => @folder}) %>',
				max_file_size : '100mb',
				max_file_count: 20, // user can add no more then 20 files at a time
				multipart: true,
				multipart_params : {authenticity_token : jQuery("input[name=authenticity_token]").val()},
		
				// Rename files by clicking on their titles
				rename: true,
		
				// Flash settings
				flash_swf_url : '<%= Engines::RailsExtensions::AssetHelpers.plugin_asset_path("redmine_dmsf", "javascripts", "plupload/plupload.flash.swf") %>'
			});
			jQuery(".plupload_scroll",uploader).resizable({
				handles: "s"
			});
			var pluploader = uploader.plupload('getUploader');
			
			pluploader.bind('FileUploaded', function(pluploader, file, response) {
				var responseObject = jQuery.parseJSON(response.response);
				var disk_filename_input = jQuery("<input/>").attr("type","hidden")
					.attr("name","uploaded[" + dmsfFileFieldCount + "][disk_filename]")
					.val(responseObject.disk_filename);
				uploader.append(disk_filename_input);
				var content_type_input = jQuery("<input/>").attr("type","hidden")
					.attr("name","uploaded[" + dmsfFileFieldCount + "][content_type]")
					.val(responseObject.content_type);
				uploader.append(content_type_input);
				var original_filename_input = jQuery("<input/>").attr("type","hidden")
					.attr("name","uploaded[" + dmsfFileFieldCount + "][original_filename]")
					.val(responseObject.original_filename);
				uploader.append(original_filename_input);
				
				if(pluploader.total.uploaded == pluploader.files.length) jQuery('#uploadform').submit();
				else dmsfFileFieldCount++;
			});
		}
		
		var dmsfFileFieldCount = 1;

		function dmsfAddFileField() {
		    if (dmsfFileFieldCount >= 10) return false
		    dmsfFileFieldCount++;

		    var f = document.createElement("input");
		    f.type = "file";
		    f.name = "uploaded_files[" + dmsfFileFieldCount + "]";
		    f.size = 30;
		
		    var p = document.getElementById("uploaded_files_fields");
		    p.appendChild(document.createElement("br"));
		    p.appendChild(f);
		}

	</script>
<% end %>